﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DATN.mds.OpportunityManagement
{
    public partial class EditOM : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Load_data();
            }
                Button2.Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa không?');");

            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {

                string name = TextBox1.Text;

                DateTime date_start = DateTime.Parse(TextBox2.Text);
                DateTime date_close = DateTime.Parse(TextBox3.Text);
                DateTime date_create = DateTime.Parse(TextBox4.Text);

                string datestart= date_start.ToString("MM/dd/yyyy");
                string dateclose= date_start.ToString("MM/dd/yyyy");
                string datecreate = date_start.ToString("MM/dd/yyyy");

                string stt = TextBox5.Text;
                double es_fu = double.Parse(TextBox6.Text);
                double es_po = double.Parse(TextBox7.Text);


                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

                string com1 = "Update Opportunities "
                    + "\n Set Name = N'" + name + "',"
                    + "DateStart = '" + datestart + "',"
                    + "DateClose = '" + dateclose + "',"
                    + "DateCreate = '" + datecreate + "',"
                    + "Status = N'" + stt + "',"
                    + "EstimatesFund = '" + es_fu + "',"
                    + "EstimatesProfit = '" + es_po + "'"
                    + "\n Where Opp_Id =" + Convert.ToInt32(Session["Opp_Id"].ToString());

                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;

                cmd.CommandText = com1;

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
            Page.Response.Redirect("~/mds.OpportunityManagement/IndexOM.aspx");
        }

        protected void Load_data()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            cnn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select * From Opportunities Where Opp_Id = " + Convert.ToInt32(Session["Opp_Id"].ToString()), cnn);

            da.Fill(ds, "Opportunities");


           
            string Text1 = ds.Tables["Opportunities"].Rows[0].ItemArray[2].ToString();
            string Text2 = ds.Tables["Opportunities"].Rows[0].ItemArray[3].ToString();
            string Text3 = ds.Tables["Opportunities"].Rows[0].ItemArray[4].ToString();
            DateTime date1 = new DateTime();
            if (Text1 != "")
            {
                date1 = Convert.ToDateTime(Text1);
                TextBox2.Text = date1.ToString("dd/MM/yyyy");
            }
            else
                TextBox2.Text = "";
            DateTime date2 = new DateTime();

            if (Text2 != "")
            {
                date2 = Convert.ToDateTime(Text2);
                TextBox3.Text = date2.ToString("dd/MM/yyyy");
            }
            else
                TextBox3.Text = "";
            DateTime date3 = Convert.ToDateTime(Text3);


            //TextBox8.Text = ds.Tables["Opportunities"].Rows[0].ItemArray[0].ToString();
            TextBox1.Text = ds.Tables["Opportunities"].Rows[0].ItemArray[1].ToString();
            
            TextBox4.Text = date3.ToString("dd/MM/yyyy");
            TextBox5.Text = ds.Tables["Opportunities"].Rows[0].ItemArray[5].ToString();
            
            decimal  txt6 = decimal.Parse(ds.Tables["Opportunities"].Rows[0].ItemArray[6].ToString());
            decimal txt7 = decimal.Parse(ds.Tables["Opportunities"].Rows[0].ItemArray[7].ToString());

            TextBox6.Text = string.Format("{0:0,0}", txt6);
            TextBox7.Text = string.Format("{0:0,0}", txt7);

            ds.Dispose();
            da.Dispose();
            cnn.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //Button2.Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa không?');");
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            string ac = "Delete From ActionPlan Where Opp_Id =" + Convert.ToInt32(Session["Opp_Id"].ToString());

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = ac;

            cnn.Open();
            cmd.ExecuteNonQuery();

            string ac1 = "Delete From Opportunities Where Opp_Id =" + Convert.ToInt32(Session["Opp_Id"].ToString());
            
            SqlCommand cmd1 = cnn.CreateCommand();
            cmd1.CommandType = CommandType.Text;
            cmd1.CommandText = ac1;
            cmd1.ExecuteNonQuery();
            
            cnn.Close();

            Page.Response.Redirect("~/mds.OpportunityManagement/IndexOM.aspx");


            //Load_data();
        }

        
    }
}